package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.ProvinceDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.ProvinceInfo;
import com.sls.game.service.ProvinceService;

/**
 * 
 * @Description：
 * @author wxg
 * @Date：2013-6-14
 * 
 */
@Service("provinceService")
public class ProvinceServiceImpl implements ProvinceService {

	private static Logger log = Logger.getLogger(ProvinceServiceImpl.class);

	private Gson gson = null;

	public ProvinceServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Resource(name = "provinceDAO")
	private ProvinceDAO provinceDAO;

	/**
	 * 记录新增
	 * 
	 * @param body
	 * @param head
	 * @return
	 */
	@Override
	public String insertInfo(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		ProvinceInfo provinceInfo = gson.fromJson(body, ProvinceInfo.class);
		try {
			String provinceName = provinceInfo.getProvinceName();
			ProvinceInfo pro = provinceDAO.queryProByProvinceName(provinceName);
			if (pro == null) { //新增
				provinceInfo.setId(UUID.randomUUID().toString());
				String inserttime = ServerDateAndTime.getNowDateTimeOfFormat();
				provinceInfo.setInserttime(inserttime);
				provinceDAO.insert(provinceInfo);
			}else { //更新
				String updatetime = ServerDateAndTime.getNowDateTimeOfFormat();
				provinceInfo.setUpdatetime(updatetime);
				provinceDAO.update(provinceInfo);
			}
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return null;
	}

	/**
	 * 记录更新
	 * 
	 * @param body
	 * @param head
	 * @return
	 */
	@Override
	public String updateInfo(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		ProvinceInfo provinceInfo = gson.fromJson(body, ProvinceInfo.class);
		try {
			String updatetime = ServerDateAndTime.getNowDateTimeOfFormat();
			provinceInfo.setUpdatetime(updatetime);
			provinceDAO.update(provinceInfo);
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return null;
	}

	/**
	 * 根据主键id，查询记录信息
	 * 
	 * @param body
	 * @param head
	 * @return
	 */
	@Override
	public String queryById(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		ProvinceInfo provinceInfo = gson.fromJson(body, ProvinceInfo.class);
		boolean flag = checkDataOfQueryInfoById(provinceInfo,
				head.getRequestId());
		if (flag) {
			ProvinceInfo proInfo = null;
			String id = provinceInfo.getId();
			proInfo = provinceDAO.queryProvinceInfoById(id);
			String resultBody = null;
			if (proInfo != null) {
				resultBody = gson.toJson(proInfo);
			}
			return resultBody;
		}

		return null;
	}

	/**
	 * 分页查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 */
	@Override
	public String queryProPagination(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		ProvinceInfo provinceInfo = gson.fromJson(body, ProvinceInfo.class);
		boolean flag = checkDataOfByPage(provinceInfo, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				Pagination<ProvinceInfo> proList = null;
				proList = provinceDAO.queryPagination(provinceInfo);
				String resultBody = null;
				if (proList != null) {
					resultBody = gson.toJson(proList);
				}
				return resultBody;
			} catch (Exception e) {
				log.error(e.toString(), e);
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}

		}
		return null;
	}

	/**
	 * 
	 * @param info
	 * @param reqId
	 * @return
	 */
	private boolean checkDataOfByPage(ProvinceInfo info, String reqId) {
		boolean flag = false;
		String currentPage = info.getCurrentPage() + "";
		String pageSize = info.getPageSize() + "";
		if ("".equals(currentPage) || currentPage == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else if ("".equals(pageSize) || pageSize == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 
	 * @param info
	 * @param reqId
	 * @return
	 */
	private boolean checkDataOfQueryInfoById(ProvinceInfo info, String reqId) {
		boolean flag = false;
		String id = info.getId();
		if ("".equals(id) || id == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

}
